package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.CheckRequest;
import org.example.pojo.Cost;
import org.example.pojo.MedicalTechnology;
import org.example.pojo.Register;
import org.example.query.CheckRequestQuery;
import org.example.service.ICheckRequestService;
import org.example.service.ICostService;
import org.example.service.IRegisterService;
import org.example.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-18
 */
@Slf4j
@RestController
@RequestMapping("/checkrequest")
public class CheckRequestController extends BaseController<CheckRequest, CheckRequestQuery> {

    @Autowired
    private ICheckRequestService checkRequestService;
    @Autowired
    ICostService costService;
    @Autowired
    IRegisterService registerService;

    @GetMapping("/reg/{rid}")
    public ResultBean getByRegId(@PathVariable("rid") Integer regId) {
        MPJLambdaWrapper<CheckRequest> queryWrapper=new MPJLambdaWrapper<>();
        queryWrapper.setAlias("cr")
                .selectAll(CheckRequest.class,"cr")
                .selectAs(MedicalTechnology::getTechName,"'medicalTechnology.techName'")
                .selectAs(MedicalTechnology::getTechPrice,"'medicalTechnology.techPrice'")
                .leftJoin(MedicalTechnology.class,"m",MedicalTechnology::getId,CheckRequest::getMedicalTechnologyId)
                .eq("register_id",regId);
        List<CheckRequest> checkRequestList= checkRequestService.list(queryWrapper);
        return ResultBean.SUCCESS(checkRequestList);
    }
    @PostMapping("/addbatch")
    public ResultBean addBatch(@RequestBody List<CheckRequest> requestList ) {
        log.info("进来添加病例");
        if(requestList==null || requestList.size()==0) {

            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        //这里再次进行点击添加的时候，由于前端没有进行清空，那个addList的缘故导致服务器报错
        Boolean b=checkRequestService.saveBatch(requestList);
        if(!b) {
            throw new AppException(400002,"添加检查申请失败");
        }
        //创建费用合集
        //如果添加检查申请成功，添加对应的给用记录
        List<Cost> costList=new ArrayList<Cost>();

        requestList.forEach(checkRequest->{
            Cost cost=new Cost(null,checkRequest.getRegisterId(),checkRequest.getMedicalTechnology().getTechName(),
                    checkRequest.getId(),"检查",checkRequest.getMedicalTechnology().getTechPrice(),
                    1,null,0,null,null,null,null);
            costList.add(cost);
        });

        //批量添加费用记录
        boolean b1= costService.saveBatch(costList);
        if(!b1) {
            throw new AppException(400001,"添加费用记录失败");
        }
        return ResultBean.SUCCESS();
    }
    @RequestMapping("/conditions")
    public ResultBean getByConitions(@RequestBody CheckRequestQuery checkRequestQuery) {
        String caseNumber=checkRequestQuery.getCaseNumber();
        if(StringUtils.isNotEmpty(caseNumber)) {
            QueryWrapper<Register> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("case_number",caseNumber)
                    .select("register_id");
            Integer id=registerService.getOne(queryWrapper).getId();
            checkRequestQuery.setRegisterId(id);
        }
        List<CheckRequest> list=checkRequestService.list(checkRequestQuery.getQueryWrapper());
        return ResultBean.SUCCESS(list);
    }
}
